<template>
	<view class="container login">
		<view class="image">
			<image src="../../static/images/Business.png"></image>
		</view>
		<view class="title">学创签电子合同</view>
		<view class="bottom-button">
			<view class="type-box">
				<view class="box">
					<view :class="['item', type == 1?'active':'']" @click="changeType(1)">个人</view>
					<view :class="['item', type == 2?'active':'']" @click="changeType(2)">企业</view>
				</view>
			</view>
			<u-button type="info" :custom-style="customStyle" color="#4787F0" text="手机号快捷登录" open-type='getPhoneNumber'
				@getphonenumber='ongetPhoneNumber'></u-button>
			<u-button type="info" style="margin-top:58rpx ;" :custom-style="customStyle" color="#4787F0" text="密码/验证码登录"
				@click="goNav('/pages/alogin/login')"></u-button>
			<view class="register" @click="goNav('/pages/alogin/register')">去注册<u-icon name="arrow-right"
					color="#ffffff" size="15"></u-icon></view>
		</view>
	</view>
</template>
<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		useStore
	} from 'vuex'
	import {
		arrayGet,
		goNav,
		goSwitchTab,
		mobileRegular,
		msg,
		pageUrl
	} from "../../utils/common";
	import {
		enterpriseLogin,
		memberWxMiniLogin,
		userWxMiniLogin
	} from "../../api/module/login";
	import {
		membercontractList
	} from "../../api/module/contract";
	import {
		onShow,
		onLoad
	} from "@dcloudio/uni-app";
	const type = ref(1)
	const store = useStore()
	const loginCode = ref('')
	const customStyle = reactive({
		width: "480rpx",
		marginTop: "58rpx",
		background: '#4787F0',
		borderRadius: '8rpx',
		border: '2rpx solid #FFFFFF !important',
		color: '#fff',
	});
	const changeType = (index) => {
		type.value = index
		store.commit('changeUserType', index)
	}
	const ongetPhoneNumber = (e) => {
		if (e.detail.errMsg != "getPhoneNumber:ok") {
			return;
		}
		const encryptedCode = e.detail.code;
		const encryptedData = e.detail.encryptedData;
		const iv = e.detail.iv;
		const Member = uni.getStorageSync("userInfo");
		const params = {
			code: loginCode.value,
			encryptedData: encryptedData,
			encryptedCode: encryptedCode,
			iv: iv,
		};
		console.log('微信数据', params);
		console.log('登录类型', store);
		switch (store.state.userType) {
			case 1:
				memberWxMiniLogin(params).then(res => {
					console.log(res);
					uni.setStorageSync("token", res.token)
					uni.setStorageSync("userInfo", res.member)
					if (res.member.set_password_status == 2) {
						goNav('/pages/alogin/setUserInfo')
						return;
					} 
					// else {
					// 	goSwitchTab('/pages/home/index');
					// }
					if (arrayGet(res, 'member.certification_status') != 1) {
						// 调用uni.showModal()函数显示确认取消提示框
						uni.showModal({
							title: '提示', // 标题
							content: '您尚未实名认证，请实名认证后再操作', // 内容
							cancelText: "忽略", // 取消按钮的文字
							confirmText: "去认证", // 确认按钮文字
							success(res) {
								if (res.confirm) {
									goNav(arrayGet(pageUrl, 'realnameUrl.1'));
									// 这里写取消操作的相关代码
								} else if (res.cancel) {
									goSwitchTab('/pages/home/index');
								}
							}
						});
						return;
					}
					membercontractList({
						limit: 10,
						sign_status: 'pending',
					}).then(response => {
						if (response.count > 0) {
							uni.showModal({
								title: '提示', // 标题
								content: '您尚有未签署的合同，请及时处理', // 内容
								cancelText: "忽略", // 取消按钮的文字
								confirmText: "去签署", // 确认按钮文字
								success(res) {
									if (res.confirm) {
										goSwitchTab('/pages/contract/index');
										// 这里写取消操作的相关代码
									} else if (res.cancel) {
										goSwitchTab('/pages/home/index');
									}
								}
							});
							return true;
						}
						goSwitchTab('/pages/home/index');
					});
				}).catch(err => {
					msg(err)
				});
				wxLogin()
				break;
			case 2:
				userWxMiniLogin(params).then(res => {
					uni.setStorageSync("token", res.token)
					uni.setStorageSync("userInfo", res.user)
					if (res.user.set_password_status == 2) {
						goNav('/pages/alogin/setUserInfo')
					} else {
						goSwitchTab('/pages/home/index');
					}
				}).catch(err => {
					uni.$u.toast(err)
				})
				wxLogin()
				break;
		}
		// 将code传递到后台服务器进行处理
		// ...
		// 根据后台服务器返回的结果，判断用户是否成功登录
		// 若成功则进行页面跳转或显示成功消息；若失败则显示错误消息
	}
	onLoad(() => {})
	const wxLogin = () => {
		wx.login({
			success(res) {
				if (res.code) {
					loginCode.value = res.code; // 获取到用户的code值
				} else {
					console.log('登录失败！' + res.errMsg);
				}
			},
			fail(error) {
				console.log('登录失败！', error);
			}
		});
	}
	const wxCheckSession = () => {
		wx.checkSession({
			success: function(res) {
				console.log('已登录', res);
				if (!loginCode.value) {
					wxLogin();
				}
			},
			fail: function(res) {
				console.log('未登录', res);
				wxLogin();
			}
		});
	}
	onShow(() => {
		// wxCheckSession();
		wxLogin();
		type.value = store.state.userType
	})
</script>
<style lang="scss" scoped src="/static/css/login/index.scss"></style>